package susebs.pb.travel_server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import susebs.pb.travel_server.domain.PageDto;
import susebs.pb.travel_server.entity.Landmarks;
import susebs.pb.travel_server.dao.LandmarksDao;
import susebs.pb.travel_server.entity.Orders;
import susebs.pb.travel_server.service.LandmarksService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 景区表(Landmarks)表服务实现类
 *
 * @author makejava
 * @since 2024-02-05 08:02:27
 */
@Service("landmarksService")
public class LandmarksServiceImpl implements LandmarksService {
    @Resource
    private LandmarksDao landmarksDao;

    /**
     * 通过ID查询单条数据
     *
     * @param lmId 主键
     * @return 实例对象
     */
    @Override
    public Landmarks queryById(Long lmId) {
        return this.landmarksDao.queryById(lmId);
    }

    /**
     * 分页查询
     *
     * @param landmarks   筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Landmarks> queryByPage(Landmarks landmarks, PageRequest pageRequest) {
        long total = this.landmarksDao.count(landmarks);
        return new PageImpl<>(this.landmarksDao.queryAllByLimit(landmarks, pageRequest), pageRequest, total);
    }

    @Override
    public PageInfo queryAllLmsByPage(PageDto pageDto) {
        //空值处理
        if(pageDto.getPage() == null || pageDto.getSize() == null){
            pageDto.setPage(1);
            pageDto.setSize(10);
        }
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<Landmarks> list = landmarksDao.queryAllLms(pageDto.getLandmarks());
        PageInfo<Landmarks> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 新增数据
     *
     * @param landmarks 实例对象
     * @return 实例对象
     */
    @Override
    public Landmarks insert(Landmarks landmarks) {
        this.landmarksDao.insert(landmarks);
        return landmarks;
    }

    /**
     * 修改数据
     *
     * @param landmarks 实例对象
     * @return 实例对象
     */
    @Override
    public Landmarks update(Landmarks landmarks) {
        this.landmarksDao.update(landmarks);
        return this.queryById(landmarks.getLmId());
    }

    /**
     * 通过主键删除数据
     *
     * @param lmId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long lmId) {
        return this.landmarksDao.deleteById(lmId) > 0;
    }
}
